﻿<%-- 
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
--%>

<%@ Page Title="" Language="C#" MasterPageFile="~/Classifieds/Main.Master" AutoEventWireup="true"
    CodeBehind="UserView.aspx.cs" Inherits="MixNP.Web.Classifieds.UserView" %>

<asp:Content ID="Content1" ContentPlaceHolderID="StyleSheetContentPlaceHolder" runat="server">
    <style type="text/css">
        .user-profile-header
        {
            font-weight: bold;
            text-align: right;
            color: #236E94;
            width: 120px;
        }
        
        .tb
        {
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ScriptsContentPlaceHolder" runat="server">
    <link rel="stylesheet" href="/Resources/Scripts/hoverzoom/jquery.hoverZoom.css" />
    <script src="/Resources/Scripts/hoverzoom/jquery.hoverZoom.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyContentPlaceHolder" runat="server">
    <h1>
        <asp:Literal ID="NickNameLiteral" runat="server" Text="Nick Bin" />
    </h1>
    <hr class="hr" />
    <table style="width: 100%;">
        <tr>
            <td style="width:204px;">
                <asp:Literal ID="ImageLiteral" runat="server" />
            </td>
            <td>
                <table class="hpad4 info-table tb">
                    <tr>
                        <td class="user-profile-header">
                            Gender :
                        </td>
                        <td>
                            <asp:Literal ID="GenderLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Address :
                        </td>
                        <td>
                            <asp:Literal ID="AddressLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Cell Number :
                        </td>
                        <td>
                            <asp:Literal ID="CellNumberLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Contact Number :
                        </td>
                        <td>
                            <asp:Literal ID="ContactNumberLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Joined On :
                        </td>
                        <td>
                            <asp:Literal ID="JoinedOnLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Member for :
                        </td>
                        <td>
                            <asp:Literal ID="MemberForLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Total Ads :
                        </td>
                        <td>
                            <asp:Literal ID="TotalAdsLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Expired Ads :
                        </td>
                        <td>
                            <asp:Literal ID="ExpiredAdsLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Withdrawn Ads :
                        </td>
                        <td>
                            <asp:Literal ID="WithdrawnAdsLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Sold :
                        </td>
                        <td>
                            <asp:Literal ID="SoldLiteral" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td class="user-profile-header">
                            Active Ads :
                        </td>
                        <td>
                            <asp:Literal ID="ActiveAdsLiteral" runat="server" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <h2>
        Ads By <asp:Literal ID="NickNameLiteral2" runat="server" />
    </h2>
    <hr class="hr" />
    <webdiyer:AspNetPager ID="TopPager" runat="server" CssClass="pager" UrlPaging="True"
        PagingButtonType="Text" NumericButtonType="Text" NavigationButtonType="Text"
        ShowNavigationToolTip="true" ShowPageIndexBox="Never" ShowPageIndex="true" AlwaysShowFirstLastPageNumber="true"
        AlwaysShow="false" UrlPageIndexName="page">
    </webdiyer:AspNetPager>
    <asp:Repeater ID="CardRepeater" runat="server">
        <ItemTemplate>
            <pes:HorizontalItemCard ID="HorizontalItemCard1" runat="server" ItemId='<%# Bind("ItemId") %>' />
        </ItemTemplate>
    </asp:Repeater>
    <asp:Literal ID="NothingLiteral" runat="server" />
    <webdiyer:AspNetPager ID="BottomPager" runat="server" CssClass="pager" UrlPaging="True"
        PagingButtonType="Text" NumericButtonType="Text" NavigationButtonType="Text"
        ShowNavigationToolTip="true" ShowPageIndexBox="Never" ShowPageIndex="true" AlwaysShowFirstLastPageNumber="true"
        AlwaysShow="false" UrlPageIndexName="page">
    </webdiyer:AspNetPager>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ScriptsContentPlaceHolderBottom"
    runat="server">
    <script type="text/javascript">
        $(document).ready(function () {
            $('.thumb img').hoverZoom({ speedView: 600, speedRemove: 400, showCaption: true, speedCaption: 600, debug: true, hoverIntent: true, loadingIndicatorPos: 'center' });
        });
    </script>
</asp:Content>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        string salt = RouteData.Values["salt"].ToString();
        string emailAddress = MixNP.BusinessLayer.Users.GetEmailAddressFromSalt(salt);
        int page = Pes.Utility.Conversion.TryCastInteger(this.Request.QueryString["page"]);

        ShowSearchResults(emailAddress, 0, page);

        this.Page.Title = "Ads by " + NickNameLiteral.Text + ". Page #" + page.ToString() + ".";
        this.ShowUserInfo(salt);
    }

    private void ShowUserInfo(string token)
    {

        if (!string.IsNullOrWhiteSpace(token))
        {
            if (MixNP.BusinessLayer.Users.IsValidToken(token))
            {
                string emailAddress = MixNP.BusinessLayer.Users.GetEmailAddressFromSalt(token);
                //TODO
                string protectProfilePicture = string.Empty;
                string sex = string.Empty;
                string userName = string.Empty;
                string nickName = string.Empty;
                string cellNumber = string.Empty;
                string contactNumber = string.Empty;
                bool displayCellNumber;
                bool displayContactNumbers;
                string addressLine1 = string.Empty;
                string addressLine2 = string.Empty;
                string addressLine3 = string.Empty;
                string city = string.Empty;
                DateTime joinedOn = DateTime.MinValue;
                double memberFor = int.MinValue;
                int totalAds = int.MinValue;
                int expiredAds = int.MinValue;
                int withdrawnAds = int.MinValue;
                int sold = int.MinValue;
                int activeAds = int.MinValue;
                
                if (!string.IsNullOrWhiteSpace(emailAddress))
                {
                    using (System.Data.DataTable table = MixNP.BusinessLayer.Profiles.GetProfile(emailAddress))
                    {
                        if (table.Rows.Count.Equals(1))
                        {

                            if (Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["ProtectProfilePicture"]).Equals(true))
                            {
                                protectProfilePicture = "1";
                            }
                            else
                            {
                                protectProfilePicture = "0";
                            }

                            sex = Pes.Utility.Conversion.TryCastString(table.Rows[0]["Sex"]);
                            userName = Pes.Utility.Conversion.TryCastString(table.Rows[0]["Name"]);
                            nickName = Pes.Utility.Conversion.TryCastString(table.Rows[0]["NickName"]);
                            cellNumber = Pes.Utility.Conversion.TryCastString(table.Rows[0]["CellNumber"]);

                            displayCellNumber = Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["DisplayCellNumber"]);

                            contactNumber = Pes.Utility.Conversion.TryCastString(table.Rows[0]["ContactNumbers"]);

                            displayContactNumbers = Pes.Utility.Conversion.TryCastBoolean(table.Rows[0]["DisplayContactNumbers"]);

                            addressLine1 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine1"]);
                            addressLine2 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine2"]);
                            addressLine3 = Pes.Utility.Conversion.TryCastString(table.Rows[0]["AddressLine3"]);
                            city = Pes.Utility.Conversion.TryCastString(table.Rows[0]["City"]);
                            joinedOn = Pes.Utility.Conversion.TryCastDate(table.Rows[0]["ConfirmedOn"]);
                            memberFor = System.DateTime.UtcNow.Subtract(joinedOn).TotalDays;
                            totalAds = Pes.Utility.Conversion.TryCastInteger(table.Rows[0]["TotalAds"]);
                            expiredAds = Pes.Utility.Conversion.TryCastInteger(table.Rows[0]["ExpiredAds"]);
                            withdrawnAds = Pes.Utility.Conversion.TryCastInteger(table.Rows[0]["WithdrawnAds"]);
                            sold = Pes.Utility.Conversion.TryCastInteger(table.Rows[0]["Sold"]);
                            activeAds = Pes.Utility.Conversion.TryCastInteger(table.Rows[0]["ActiveAds"]);
                            

                            NickNameLiteral.Text = nickName;
                            NickNameLiteral2.Text = nickName;
                            CellNumberLiteral.Text = "Hidden";
                            ContactNumberLiteral.Text = "Hidden";
                            
                            JoinedOnLiteral.Text = joinedOn.ToString() + " (UTC)";
                            MemberForLiteral.Text = memberFor.ToString();
                            TotalAdsLiteral.Text = totalAds.ToString();
                            ExpiredAdsLiteral.Text = expiredAds.ToString();
                            WithdrawnAdsLiteral.Text = withdrawnAds.ToString();
                            SoldLiteral.Text = sold.ToString();
                            ActiveAdsLiteral.Text = activeAds.ToString();

                            if (!userName.Equals(nickName))
                            {
                                NickNameLiteral.Text = nickName + " (nickname)";
                            }

                            if (displayCellNumber)
                            {
                                CellNumberLiteral.Text = cellNumber;
                            }

                            if (displayContactNumbers)
                            {
                                ContactNumberLiteral.Text = contactNumber;
                            }

                            GenderLiteral.Text = sex;

                            AddressLiteral.Text = (addressLine1 + " " + addressLine2 + " " + addressLine3 + " " + city).Trim();


                            string filePath = ResolveUrl("~/Handlers/GetProfilePicture.ashx?Path=" + token + "&RequestId=" + Guid.NewGuid().ToString());


                            StringBuilder html = new StringBuilder();
                            html.Append("<ul class=\"bpad8 thumb\">");
                            html.Append("<li>");
                            html.Append("<a href='" + filePath + "'>");
                            html.Append("<img class='thumb2' src='" + filePath + "&H=287&W=200' alt='" + nickName + "' />");
                            html.Append("</a>");
                            html.Append("</li>");
                            html.Append("</ul>");

                            ImageLiteral.Text = html.ToString();

                            return;
                        }
                    }
                }
            }
        }


    }


    private void ShowSearchResults(string emailAddress, short sort, int pageNumber)
    {
        string strConn = ConfigurationManager.ConnectionStrings["PESConnectionString"].ConnectionString;
        System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(strConn);

        System.Data.SqlClient.SqlCommand Cmd = new System.Data.SqlClient.SqlCommand("dbo.SearchAdByUser", cn);
        Cmd.CommandType = System.Data.CommandType.StoredProcedure;
        System.Data.SqlClient.SqlDataReader dr;

        bool hasRows = false;

        if (sort.Equals(short.MinValue)) { sort = 0; }
        if (pageNumber.Equals(int.MinValue)) { pageNumber = 0; }


        if (pageNumber.Equals(0)) { pageNumber = 1; }

        Cmd.Parameters.Add("@EmailAddress", System.Data.SqlDbType.NVarChar).Value = emailAddress;
        Cmd.Parameters.Add("@PageNumber", System.Data.SqlDbType.Int).Value = pageNumber;
        Cmd.Parameters.Add("@Order", System.Data.SqlDbType.SmallInt).Value = sort;

        Cmd.Parameters.Add("@ItemCount", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.Output;

        cn.Open();
        dr = Cmd.ExecuteReader();


        CardRepeater.DataSource = dr;
        CardRepeater.DataBind();
        hasRows = dr.HasRows;
        dr.Close();
        cn.Close();

        Int32 _totalRecords = Convert.ToInt32(Cmd.Parameters["@ItemCount"].Value);

        TopPager.RecordCount = _totalRecords;
        TopPager.PageSize = 20;

        BottomPager.RecordCount = _totalRecords;
        BottomPager.PageSize = 20;

        TopPager.ShowDisabledButtons = false;
        BottomPager.ShowDisabledButtons = false;
        if (_totalRecords.Equals(0) || !hasRows)
        {
            TopPager.Visible = false;
            BottomPager.Visible = false;
            NothingLiteral.Text = "<h2>Nothing found. :(</h2>";
        }

    }

</script>
